package com.wallet.my.controller;

import com.wallet.my.contract.Web3jContract.PaiMai;
import com.wallet.my.controller.model.ContractFindRequest;
import com.wallet.my.controller.model.TakeBackMoneyRequest;
import com.wallet.my.controller.model.TransContractRequest;
import com.wallet.my.dao.pojo.ContractDO;
import com.wallet.my.dao.pojo.ContractRecordDO;
import com.wallet.my.service.ContractService;
import com.wallet.my.service.ListenerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能合约相关接口        后面写前端的时候需要解决金额的换算单位问题
 **/
@RestController
@RequestMapping("/Api")
public class ContractController {
    @Autowired
    private ContractService contractService;
    @Autowired
    private ListenerService listenerService;

//  查询已发布的所有拍卖合约
    @GetMapping("/getPaiMaiContracts")
    public List<ContractDO> getPaiMaiContracts(){
        return contractService.getPaiMaiContracts();
    }


    //    部署智能合约
    @PostMapping("/newPaiMaiContract")
    public Map<String,Object> newPaiMaiContract(@RequestBody TransContractRequest transContractRequest){
        return contractService.newPaiMaiContract(transContractRequest);
    }

//    调用合约，出价
    @PostMapping("/paiMaiBid")
    public Map<String,Object> paiMaiBid(@RequestBody TransContractRequest transContractRequest){
    return contractService.paiMaiBid(transContractRequest);
    }

//    查询当前拍卖交易明细，通过事件获取
    @PostMapping("/infoPaiMaiContract")
    public List<ContractRecordDO> infoPaiMaiContract(@RequestBody ContractFindRequest contractFindRequest){
        return contractService.getContractRecordByContractAddress(contractFindRequest.getContractAddress());
    }

    //    查询当前拍卖最终赢家,通过事件获取
    @PostMapping("/endPaiMaiContract")
    public List<PaiMai.AuctionEndedEventResponse> endPaiMaiContract(@RequestBody ContractFindRequest contractFindRequest){
        return contractService.endPaiMaiContract(contractFindRequest.getContractAddress());
    }

    //    查询拍卖结束时间
    @PostMapping("/paiMaiEndTime")
    public Map<String, Object> paiMaiEndTime(@RequestBody ContractFindRequest contractFindRequest){
        return contractService.paiMaiEndTime(contractFindRequest.getContractAddress());
    }

    //    查询拍卖受益人
    @PostMapping("/paiMaiBeneficiary")
    public Map<String, Object> paiMaiBeneficiary(@RequestBody ContractFindRequest contractFindRequest){
        return contractService.paiMaiBeneficiary(contractFindRequest.getContractAddress());
    }

    //    拍卖当前最高出价人地址和出价
    @PostMapping("/paiMaiHighest")
    public Map<String, Object> paiMaiHighest(@RequestBody ContractFindRequest contractFindRequest){
        return contractService.paiMaiHighest(contractFindRequest.getContractAddress());
    }

    //    出价人赎回金额
    @PostMapping("/paiMaiWithdraw")
    public Map<String, Object> paiMaiWithdraw(@RequestBody TakeBackMoneyRequest takeBackMoneyRequest){
        String privateKey = takeBackMoneyRequest.getPrivateKey();
        String contractAddress = takeBackMoneyRequest.getContractAddress();
        return contractService.paiMaiWithdraw(privateKey,contractAddress);
    }

    //    拍卖结束，拍卖人取回金额
    @PostMapping("/paiMaiClose")
    public Map<String, Object> paiMaiClose(@RequestBody TakeBackMoneyRequest takeBackMoneyRequest){
        String privateKey = takeBackMoneyRequest.getPrivateKey();
        String contractAddress = takeBackMoneyRequest.getContractAddress();
        return contractService.paiMaiAuctionClose(privateKey,contractAddress);
    }

//    开启监听该合约的出价交易
    @PostMapping("/listenPaiMaiRecord")
    public Map<String, Object> listenPaiMaiRecord(@RequestBody ContractFindRequest contractFindRequest){
        listenerService.listenPaiMaiRecord(contractFindRequest.getContractAddress());
        HashMap<String, Object> map = new HashMap<>();
        map.put("message", "已开始监听该合约交易，请继续其他操作");
        return map;
    }














}
